#pragma once

#include <iostream>
#include "protocol.hpp"

enum
{
    Div_Zero = 1,
    Mod_Zero,
    Unknow_Oper
};

class ServerCal
{
public:
    ServerCal()
    {
    }
    Respond CalculatorHelper(const Request &req)
    {
        Respond res(0, 0);
        switch (req._op)
        {
        case '+':
            res._result = req._x + req._y;
            break;
        case '-':
            res._result = req._x - req._y;
            break;
        case '*':
            res._result = req._x * req._y;
            break;
        case '/':
        {
            if (req._y == 0)
            {
                res._code = Div_Zero;
            }
            else
            {
                res._result = req._x / req._y;
            }
        }
        break;
        case '%':
        {
            if (req._y == 0)
            {
                res._code = Mod_Zero;
            }
            else
            {
                res._result = req._x % req._y;
            }
        }
        break;
        default:
            res._code = Unknow_Oper;
            break;
        }

        return res;
    }
    std::string Calculator(std::string &package)
    {
        Request req;
        std::string contect;
        bool r = Decode(package, &contect);
        if (!r)
            return "";
        // 反序列化
        r = req.DeSerialize(contect);
        if (!r)
            return "";

        Respond res = CalculatorHelper(req);

        // 序列化
        contect = "";
        res.Serialize(&contect);
        // 添加报头
        contect = Encode(contect);

        return contect;

        
    }
    ~ServerCal()
    {
    }
};